{template 'header'}

<body>
<div class="x-body">
    <form class="layui-form">
        <input type="hidden" name="id" value="{$task['id']}">
        <div class="layui-form-item">
            <label for="title" class="layui-form-label">
                <span class="x-red">*</span>任务标题
            </label>
            <div class="layui-input-block">
                <input type="text" id="title" name="title" required="" lay-verify="title"
                       autocomplete="off" class="layui-input" value="{$task['title']}">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label"><span class="x-red"></span>任务缩略图</label>
            <div class="layui-input-block">
                <input type="hidden" name="thumb" value="{$task['thumb']}" id="thumb">
                <div id="preview-thumb">
                    {if !empty($task['thumb'])}
                    <img src="{php echo cmf_get_upload_url($task['thumb'])}" alt="" width="200" height="150">
                    {/if}
                </div>
                <button type="button" class="layui-btn" id="thumb-btn">
                    <i class="layui-icon">&#xe654;</i>上传
                </button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">选择任务分类</label>
            <div class="layui-input-block">
                <select name="cateid" lay-verify="required">
                    <option value="0">|--默认分类(不分类)</option>
                    {loop $catelist $item}
                    <option value="{$item['id']}" {if $item['id']==$task['cateid']}selected{/if}>|{php echo str_repeat('--',$item['lv'])}{$item['name']}</option>
                    {/loop}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="task_money" class="layui-form-label">
                <span class="x-red">*</span>任务金额
            </label>
            <div class="layui-input-inline">
                <input type="text" id="task_money" name="task_money" required="" lay-verify="task_money"
                       autocomplete="off" class="layui-input" value="{$task['task_money']}">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>必填
            </div>
        </div>
        <div class="layui-form-item">
            <label for="begin_time" class="layui-form-label">
                <span class="x-red">*</span>任务开始时间
            </label>
            <div class="layui-input-inline">
                <input class="layui-input" autocomplete="off" placeholder="任务开始时间" name="begin_time" id="begin_time"
                       value="{php echo date('Y-m-d H:i:s',$task['begin_time'])}" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="end_time" class="layui-form-label">
                <span class="x-red">*</span>任务结束时间
            </label>
            <div class="layui-input-inline">
                <input class="layui-input" autocomplete="off" placeholder="任务结束时间" name="end_time" id="end_time"
                       value="{php echo date('Y-m-d H:i:s',$task['end_time'])}" autocomplete="off">
            </div>
        </div>
        <!--<div class="layui-form-item">-->
            <!--&lt;!&ndash;<label for="task_link" class="layui-form-label">&ndash;&gt;-->
                <!--&lt;!&ndash;<span class="x-red">*</span>链接地址&ndash;&gt;-->
            <!--&lt;!&ndash;</label>&ndash;&gt;-->
            <!--<div class="layui-input-block">-->
                <!--<input type="text" id="task_link" name="task_link" required="" lay-verify="task_link"-->
                       <!--autocomplete="off" class="layui-input" value="">-->
            <!--</div>-->
        <!--</div>-->
        <div class="layui-form-item">
            <label for="task_link" class="layui-form-label">
                <span class="x-red">*</span>验证依据
            </label>
            <div class="layui-input-block">
                <input type="text" id="validate" name="validate" required="" lay-verify="validate"
                       autocomplete="off" class="layui-input" value="{$task['validate']}">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="news_from" class="layui-form-label">
                <span class="x-red">*</span>新闻来源
            </label>
            <div class="layui-input-block">
                <input type="text" id="news_from" name="news_from" required="" lay-verify="news_from"
                       autocomplete="off" class="layui-input" value="{$task['news_from']}">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="release_num" class="layui-form-label">
                <span class="x-red"></span>发布数量
            </label>
            <div class="layui-input-inline">
                <input type="text" id="release_num" name="release_num" required="" lay-verify="release_num"
                       autocomplete="off" class="layui-input" value="{$task['release_num']}">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red"></span>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="receive_num" class="layui-form-label">
                <span class="x-red"></span>已抢数量
            </label>
            <div class="layui-input-inline">
                <input type="text" id="receive_num" name="receive_num" required="" lay-verify="receive_num"
                       autocomplete="off" class="layui-input" value="{$task['receive_num']}">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red"></span>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="remark" class="layui-form-label">
                <span class="x-red"></span>任务描述
            </label>
            <div class="layui-input-block">
                <script id="remark_editor" type="text/plain" style="width:80%;height:100px;">{php echo htmlspecialchars_decode($task['remark'])}</script>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label">
            </label>
            <button class="layui-btn" lay-filter="save" lay-submit="">
                保存
            </button>
        </div>
    </form>
</div>
<script type="text/javascript" src="{MODULE_URL}static/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="{MODULE_URL}static/ueditor/ueditor.all.min.js"></script>
<script type="text/javascript" src="{MODULE_URL}static/ueditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript">
    var ue1 = UE.getEditor('remark_editor');
</script>
<script>
    // 设置最小可选的日期
    function minDate() {
        var now = new Date();
        return now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate();
    }

    layui.use('upload', function () {
        var upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#thumb-btn', //绑定元素
            url: '{php echo $this->createWebUrl("task_list",["op"=>"upload_thumb"])}', //上传接口
            size: 4 * 1024,//4M
            done: function (res) {
                if (res.code) {
                    var html = "<img src='" + res.data + "' width='200' height='150'>"
                    $('#preview-thumb').html(html)
                    $('#thumb').val(res.data)
                } else {
                    layer.msg(res.msg, {icon: 0});
                }
            }, error: function () {
                layer.msg("网络异常，请稍后重试", {icon: 0});
            }
        });
    });
    layui.use('laydate', function () {
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#begin_time', //指定元素
            type: 'datetime'
        });

        //执行一个laydate实例
        laydate.render({
            elem: '#end_time', //指定元素
            type: 'datetime',
            min: minDate()
        });
    });
    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;

        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 2) {
                    return '任务标题至少得2个字符啊';
                }
            },
            thumb: function (value) {
                if (value == '') {
                    return '请上传缩略图';
                }
            },
            task_money: function (value) {
                if (value <= 0) {
                    return '任务金额必须大于零';
                }
            },
            end_time: function (value) {
                var d = Date.parse(value)
                if (d < new Date().getTime()) {
                    return '任务结束时间不能小于当前时间'
                }
            },
            validate: function (value) {
                if (value == '') {
                    return '验证依据不能为空';
                }
            },
        });

        //监听提交
        form.on('submit(save)', function (data) {
            var form_data = data.field;
            if (Date.parse(form_data.begin_time >= Date.parse(form_data.end_time))) {
                layer.msg('结束时间不能小于开始时间', {icon: 0});
            }
            //发异步，把数据提交给php
            var url = '{php echo $this->createWebUrl("task_list",array("op"=>"edit"))}'
            $.post(url, form_data, function (ret) {
                console.log(ret)
                if (ret.code) {
                    layer.msg(ret.msg, {icon: 6}, function () {
                        //关闭当前frame
                        x_admin_close();
                        // 可以对父窗口进行刷新
                        x_admin_father_reload();
                    });
                } else {
                    layer.msg(ret.msg, {icon: 0});
                }
            }, 'json')
            return false;
        });
    });
</script>
<script>var _hmt = _hmt || [];
(function () {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();</script>
</body>

</html>